package org.sky.sys.util;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.sky.autoconfigure.SkyProperties;
import org.sky.configuration.ApplicationContextRegister;
import org.sky.security.support.SecurityUser;
import org.sky.sys.entity.SysUser;
import org.springframework.context.ApplicationContext;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.context.ContextLoader;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

/**
 * @author weifx
 * @Title: BspUtils
 * @ProjectName springboot-platform
 * @Description: TODO{platform平台工具类}
 * @date 2019/3/1 10:48
 * @Version 1.0
 */
public class BspUtils {
    /**
     * 获取当前登录用户
     *
     * @return
     */
    public static SecurityUser getLoginUser() {
        Object obj = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        if (obj instanceof SecurityUser) {
            SecurityUser user = (SecurityUser) obj;
            return user;
        } else {
            return null;
        }
    }
    /**
     * 根据角色编号判断用户是否具有角色
     * @param roleCode
     * @return
     */
    public static boolean haveUserRole(String roleCode) {
        List<GrantedAuthority> list = (List<GrantedAuthority>) SecurityContextHolder.getContext().getAuthentication().getAuthorities();
        for(GrantedAuthority au:list) {
            if(au.getAuthority().equals(roleCode)) {
                return true;
            }
        }
        return false;
    }
    /**
     * 根据id获取spring注册bean
     *
     * @param beanId
     * @return
     */
    public static Object getBean(String beanId) {
        //WebApplicationContext wac = ContextLoader.getCurrentWebApplicationContext();
        ApplicationContext ac = ApplicationContextRegister.getApplicationContext();
        return ac.getBean(beanId);
    }

    public static <T> T getBean(Class<T> clazz) {
        //WebApplicationContext wac = ContextLoader.getCurrentWebApplicationContext();
        ApplicationContext ac = ApplicationContextRegister.getApplicationContext();
        return ac.getBean(clazz);
    }

    /**
     * 获取session
     *
     * @return
     */
    public static HttpSession getHttpSession() {
        HttpSession session = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest()
                .getSession(true);
        return session;
    }

    /**
     * 获取request
     *
     * @return
     */
    public static HttpServletRequest getHttpRequest() {
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes())
                .getRequest();
        return request;
    }

    /**
     * 是否开启权限过滤 开发模式或者登陆用户是管理员或者超级管理员时显示所有菜单
     * @return
     */
    public static boolean isSecurityFilter() {
        String userCode = null;
        if (null != BspUtils.getLoginUser()) {
            userCode = BspUtils.getLoginUser().getCode();
        }
        SkyProperties skyProperties = BspUtils.getBean(SkyProperties.class);
        if (!skyProperties.isProductModel() || "ADMIN".equals(userCode.toUpperCase())
                || "SUPERADMIN".equals(userCode.toUpperCase())) {
            return false;
        }
        return true;
    }
}
